package com.jp.knowledge.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.callback.ItemDragHelperCallback;
import com.jp.knowledge.dialog.ComPopuWindow;
import com.jp.knowledge.model.HeadLine;
import com.jp.knowledge.util.L;

import java.util.List;

/**
 * Created by jiayen on 16/10/9.
 */

public class HeadItemAdapter extends RecyclerView.Adapter<HeadItemAdapter.ViewHolder> implements ItemDragHelperCallback.OnItemMoveListener, View.OnClickListener {


    private ItemTouchHelper itemTouchHelper;
    private ItemDragHelperCallback itemDragHelperCallback;
    private Context mContext;
    private List<HeadLine> data;
    private OnItemListerner itemListerner;
    private boolean isOpenTopic;//代表是打开话题过来的

    private HeadLine tempLine;

    private ComPopuWindow.DragItemHelper  dragItemHelper;

    private boolean isTopicFramgent;//toPFragment过来的
    public HeadItemAdapter(Context context, List<HeadLine> infos) {
        this.mContext = context;
        this.data = infos;
    }

    public void setDragItemHelper(ComPopuWindow.DragItemHelper dragItemHelper) {
        this.dragItemHelper = dragItemHelper;
    }

    public void setTopicFramgent(boolean topicFramgent) {
        isTopicFramgent = topicFramgent;
    }

    public void setOpenTopic(boolean openTopic) {
        isOpenTopic = openTopic;
    }

    public void update(List<HeadLine> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    public void setItemListerner(OnItemListerner itemListerner) {
        this.itemListerner = itemListerner;
    }

    public void setItemTouchHelper(ItemTouchHelper itemTouchHelper) {
        this.itemTouchHelper = itemTouchHelper;
    }

    public void setItemDragHelperCallback(ItemDragHelperCallback itemDragHelperCallback) {
        this.itemDragHelperCallback = itemDragHelperCallback;
    }

    protected int getLayoutId() {
        if(isTopicFramgent){
            return R.layout.tag_item;
        }else{
            return R.layout.activity_sort_head_item;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(getLayoutId(), parent, false);

        ViewHolder holder = new ViewHolder(view);

        holder.itemView.setOnClickListener(this);
//        if(position!=0) {
//            dragView(holder);
//        }
        return holder;
    }

    private void dragView(final ViewHolder holder) {
        if (itemTouchHelper != null) {
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    long curTime = System.currentTimeMillis();//记录当前时间
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            curTime = System.currentTimeMillis();//记录当前时间
                            break;
                        case MotionEvent.ACTION_UP:
//                            long dif = System.currentTimeMillis()-curTime;
//                            if(dif<1000){//触屏时间小于一秒  则认为是单机事件
//                                return false;
//                            }
                            Log.e("=====dragView===","排序完成");
                            break;
                        case MotionEvent.ACTION_MOVE:
                            Log.e("=====dragView===","移动完成");
                            break;
                        default:
                            break;
                    }

                    itemTouchHelper.startDrag(holder);
                    return false;
                }
            });

        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        tempLine = data.get(position);
        holder.itemView.setTag(position);
        holder.name.setText(tempLine.getTypeName());

//        if(position==0&&!isOpenTopic){//第一个默认不能拖动 且置灰色
//            holder.name.setTextColor(mContext.getResources().getColor(R.color.font_gray));
//        }
        if(isTopicFramgent){//topcifragment使用其他的背景颜色
            holder.name.setTextColor(mContext.getResources().getColor(R.color.font_black_think));
            holder.name.setBackgroundResource(R.drawable.btn_rbg_gray_fill);
        }

        if (isOpenTopic) {//表示是话题对话框过来的
            if (tempLine.isCheck()) {//选中
                holder.name.setTextColor(mContext.getResources().getColor(R.color.white));
                holder.name.setBackgroundResource(R.drawable.btn_blue);
            } else {
                holder.name.setTextColor(mContext.getResources().getColor(R.color.gray_deep));
                holder.name.setBackgroundResource(R.drawable.btn_rbg_gray);
            }
        }
//        if(position != 0)
            dragView(holder);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {

//        if(toPosition!=0&&fromPosition!=0){
            notifyItemMoved(fromPosition, toPosition);
            // 排序数据
            HeadLine headLine = data.remove(fromPosition);
            data.add(toPosition, headLine);
            if(dragItemHelper!=null)
                dragItemHelper.drag(fromPosition, toPosition);
//        }
        //延时刷新数据状态
//        handler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                handler.sendEmptyMessage(0);
//            }
//        },500);
        return true;
    }

    @Override
    public void moveFinish() {
        dragItemHelper.dragFinish();
    }

    public List<HeadLine> getData() {
        return data;
    }

    @Override
    public void onClick(final View view) {
//        notifyDataSetChanged();//点击前先刷新状态
//        int postion = (int) view.getTag();
//        if (itemListerner != null)
//            itemListerner.onItemClick(view, postion);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView name;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.item_name);
        }
    }



    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            notifyDataSetChanged();
        }
    };

}
